package com.jing.cloud.service.api.controller;


import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.jing.cloud.service.api.util.ThreadLocalUtil;
import com.jing.cloud.service.method.Method;
import com.jing.cloud.service.methodfactory.MethodFactory;
import com.jing.cloud.service.user.bean.Ticket;
import com.jing.cloud.service.user.method.GetUsers;
import com.jing.cloud.service.user.method.Login;
import com.jing.cloud.service.user.method.RegisterUser;
import com.jing.cloud.service.user.method.param.GetUserBean;
import com.jing.cloud.service.user.method.param.LoginBean;
import com.jing.cloud.service.user.method.param.User4Reg;
import com.jing.cloud.util.ErrCode;
import com.jing.cloud.service.Res;
import com.jing.cloud.util.RspUtil;
import org.apache.curator.shaded.com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * Created by 29017 on 2017/9/7.
 */
@RestController
@RequestMapping("api")
public class ApiController {

    @Autowired
    private RegisterUser registerUser;

    @Autowired
    private Login login;


    @RequestMapping("register")
    public Object register(User4Reg user4Reg) throws Exception {
        return registerUser.validAndRun(user4Reg);
    }

    @RequestMapping("login")
    public Object login(LoginBean loginBean) throws Exception {
        return login.validAndRun(loginBean);
    }

    @RequestMapping("serverInfo")
    public Object serverInfo() throws Exception {
        Map<String, Object> map = Maps.newHashMap();
        map.put("service", "im");
        return RspUtil.success(map);
    }

    @Autowired
    GetUsers getUsers;

    @RequestMapping("myInfo")
    public Object myInfo() throws Exception {
        Ticket ticket = ThreadLocalUtil.getTicket();
        List<Long> userIds = Lists.newArrayList();
        userIds.add(ticket.getUserId());
        GetUserBean getUserBean = new GetUserBean();
        getUserBean.setUserIds(userIds);
        getUserBean.setWithAccount(true);
        return getUsers.validAndRun(getUserBean);
    }

    @RequestMapping("getUsers")
    public Object getUsers(@RequestBody GetUserBean getUserBean) throws Exception {
        return getUsers.validAndRun(getUserBean);
    }

    @RequestMapping("index")
    public Object action(@RequestBody ApiBean apiBean) throws Exception {
        Res res = null;
        @SuppressWarnings("unchecked")
        Method<Object> method = MethodFactory.getMethod(apiBean.getMethodName());
        if (method == null) {
            // 方法没有找到，方法没有注册
            res = RspUtil.error(ErrCode.METHOD_NOT_FOUND);
        } else {
            method.setUserId(ThreadLocalUtil.getTicket().getUserId());
            Class<Object> clazz = method.getClazz();
            // json解析是否有成功
            boolean jsonParsed = false;
            // 暂定参数为json格式字符串
            Object input = null;
            try {
                input = JSON.toJavaObject(apiBean.getData(), clazz);
                jsonParsed = true;// json 解析成功
            } catch (Exception e) {
                // json 解析失败
                res = RspUtil.error(ErrCode.JSON_PARSE_ERROR, e);
            }
            if (jsonParsed) {

                // 参数校验
                res = method.validAndRun(input);
            }
        }
        return res;
    }


}
